﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
/*sunyuan-20190926
 参数：
1、获取数据库参数配置修改保存
2、执行的SQL语句；
3、生成的附件存放目录；
4、发送时间；
5、接收人：
邮箱服务器配置参数：
*/
namespace AutoSendMail
{
        public partial class DataSource : Form
    {
        private log4net.ILog log = log4net.LogManager.GetLogger("ValuesController");
        public DataSource()
        {
            InitializeComponent();
        }

    
          private void Button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = ConfigurationManager.AppSettings["connectionstring"];
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            config.AppSettings.Settings["connectionstring"].Value = textBox1.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            log.Info("保存新的数据库连接！");
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.AppSettings["connectionstring"];
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connectionString);
                //打开数据库连接
                conn.Open();
                MessageBox.Show("数据库连接成功！");
                log.Info("数据库连接成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库连接失败！" + ex.Message);
                log.Error("数据库连接失败!");
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }
        private void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}
